package zirao.base.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.Toast;

/**
 * Created by ${lixiaonan} on 2018/7/31.
 */

public class BaseUtils {

    private static Context mContext;

    public static Context getmContext() {
        return mContext;
    }

    public static void init(Context context) {
        mContext = context;
    }


    public interface CountDownInterface {
        void onTick(int var1);

        void onFinish();
    }

    public interface DelayInterface {
        void delayCallBack();
    }

    public static void delayCallBack(int seconds, final DelayInterface delayinterface) {
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                delayinterface.delayCallBack();
            }
        };
        handler.postDelayed(runnable, (long)(seconds * 1000));
    }

    public static void delayCallBack(float delayMillis, final DelayInterface delayinterface) {
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            public void run() {
                delayinterface.delayCallBack();
            }
        };
        handler.postDelayed(runnable, (long)((int)(delayMillis * 1000.0F)));
    }

    public static void countDown(int initSecond, CountDownInterface countDownInterface) {
        countDownFun(initSecond, countDownInterface);
    }

    private static void countDownFun(final int initSecond, final CountDownInterface countDownInterface) {
        delayCallBack(1, new DelayInterface() {
            public void delayCallBack() {
                int secondNow = initSecond - 1;
                if(secondNow == 0) {
                    countDownInterface.onFinish();
                } else {
                    countDownInterface.onTick(secondNow);
                    BaseUtils.countDownFun(secondNow, countDownInterface);
                }

            }
        });
    }

    public static boolean isEmpty(String input) {
        return input == null || "".equals(input) || TextUtils.isEmpty(input) || input.equals("null");
    }

    public static int getAppVersionCode(Context context) {
        int localVersionCode = 0;

        try {
            PackageInfo packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(context.getPackageName(), 0);
            localVersionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException var4) {
            var4.printStackTrace();
        }

        return localVersionCode;
    }

    private static Toast mToast = null;

    public static void showToast(Context context, String msg, int duration) {
        if(mToast == null) {
            mToast = Toast.makeText(context, msg, duration);
        } else {
            mToast.setText(msg);
        }

        mToast.show();
    }

    public static void showToast(Context context, String msg) {
        if(mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }

        mToast.show();
    }

    public static boolean getPermission(Activity context, String permissionDescribe, int requestCode, boolean mustGrant) {
        boolean result = false;
        if(Build.VERSION.SDK_INT >= 23) {
            if(ContextCompat.checkSelfPermission(context, permissionDescribe) == 0) {
                result = true;
            } else if(mustGrant) {
                ActivityCompat.requestPermissions(context, new String[]{permissionDescribe}, requestCode);
            } else {
                result = false;
            }
        } else {
            result = true;
        }

        return result;
    }
}
